package demo.jdk.features.jdk7;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author Ranchoz  2018/9/9 12:29
 **/
public class SuppressExample1 {

	public static void main(String[] args) throws BaseException {
		ReadFile reader = new ReadFile();
		reader.read("D:\\document\\file.text");
	}

	/**
	 * 记录异常，不被淹没
	 * addSuppressed
	 */
	static class ReadFile {
		public void read(String filename) throws BaseException {
			FileInputStream input = null;
			IOException readException = null;
			try {
				input = new FileInputStream(filename);
			} catch (IOException ex) {
				readException = ex;
			} finally {
				if (input != null) {
					try {
						input.close();
					} catch (IOException ex) {
						if (readException == null) {
							readException = ex;
						}else{
							//使用java7的
							readException.addSuppressed(ex);
						}
					}
				}
				if (readException != null) {
					throw new BaseException(readException);
				}
			}
		}
	}
}
